import 'package:flutter/material.dart';
import 'package:ls_work/a_elder/models/elder_care_entity.dart';
import 'package:ls_work/a_elder/page/elder_care_detail.dart';
// import 'package:ls_work/a_elder/page/elder_detail_page.dart';
import 'package:ls_work/mvp/svp_progress.dart';
import 'package:ls_work/net/HttpUtil.dart';
import 'package:ls_work/net/net.dart';
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/res/styles.dart';
import 'package:ls_work/util/app_navigator.dart';
import 'package:ls_work/util/toast.dart';

class ElderCarePlayPage extends StatefulWidget {
  final int? customerId;
  ElderCarePlayPage({Key? key, this.customerId});
  @override
  _ElderCarePlayState createState() => _ElderCarePlayState();
}

class _ElderCarePlayState extends State<ElderCarePlayPage> {
  List<ElderCareInfo> dataList = [];

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _fectData();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ListView.separated(
      itemCount: dataList.length,
      itemBuilder: (_, int index) {
        return InkWell(
          onTap: () {
            AppNavigator.push(
                context,
                ElderCareDetailPage(
                  id: dataList[index].id,
                ));
          },
          child: Container(
            padding: EdgeInsets.all(15),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Text(
                  dataList[index].launchTypeName??"",
                  style: TextStyles.textDark16,
                ),
                SizedBox(
                  width: 10,
                ),
                Expanded(
                  child: Text(
                    dataList[index].statusName??"",
                    style: TextStyles.textGray14,
                  ),
                ),
                Text(
                  dataList[index].updatedDate??"",
                  style: TextStyles.textGray12,
                ),
              ],
            ),
          ),
        );
      },
      separatorBuilder: (_, int index) {
        return Container(
          width: double.infinity,
          height: 1,
          color: Colours.bg_color,
        );
      },
    );
  }

  ///获取数据
  _fectData() {
    var map = {
      'customerId': widget.customerId,
    };
    SVProgressHUD page = SVProgressHUD(context);
    page.showProgress();
    HttpUtil.instance
        .getDio()
        .post(HttpApi.servicePlanElderList, data: requestParams(map))
        .then((r) {
      ///
      page.closeProgress();
      if (r.statusCode == 200) {
        Map<String, dynamic> map = json.decode(r.toString());
        print(map);
        if (map["code"].toString() == "0") {
          if (map['data'] != null) {
            ElderCareEntity model = ElderCareEntity.fromJson(map['data']);
            setState(() {
              dataList = model.rows??[];
            });
          } else {
            Toast.show(map["msg"]);
          }
        }
      } else {
        Toast.show(r.statusCode.toString());
      }
    }).catchError((e) {
      page.closeProgress();
      print(e.toString());
      Toast.show(getErrorMessage(e));
    });
  }
}
